<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.context.ApplicationContext" %>
<%@ page import="com.sz.common.ICommonDAO" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

String url_right = "ticket/checkTicket.page";

String style1 = "text-align:right;font-size: 12px;";
String style2 = "text-align:left;font-size:12px; width:140px";

String region_id = request.getSession().getAttribute("regionid").toString();

ServletContext servletContext = request.getSession().getServletContext(); 
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
ICommonDAO dao = (ICommonDAO) context.getBean("CommonDAO");

List flags = dao.getDicValueList("CHECK_FLAG");

//查到最顶级
StringBuffer buf = new StringBuffer();
buf.append("select * from region where region_level = 1 start with region_id = ");
buf.append(region_id);
buf.append(" connect by  prior parent_id =  region_id");
Map top = (Map)dao.queryForObject(buf.toString());

//自最顶级向下查
List list = new ArrayList();
if(top != null) {
	buf.delete(0, buf.length());
	buf.append("select * from region start with region_id = ");
	buf.append(top.get("REGION_ID").toString());
	buf.append(" connect by  parent_id = prior region_id");
	list = dao.queryForList(buf.toString());
}

String province_id = "";
String province_name = "";
for(int i = 0; i < list.size(); ++i) {
	Map map = (Map)list.get(i);
	if(map.get("REGION_LEVEL").toString().equals("1")) {
		province_id = map.get("REGION_ID").toString();
		province_name = map.get("REGION_NAME").toString();
		break;
	}
}

List city_list = new ArrayList();
for(int i = 0; i < list.size(); ++i) {
	Map map = (Map)list.get(i);
	if(map.get("REGION_LEVEL").toString().equals("2")) {
		city_list.add(map);
	}
}

List area_list = new ArrayList();
for(int i = 0; i < list.size(); ++i) {
	Map map = (Map)list.get(i);
	if(map.get("REGION_LEVEL").toString().equals("3")) {
		area_list.add(map);
	}
}

//根据登陆区域查商圈
buf.delete(0, buf.length());
buf.append("select * from business_group t where t.region_id in (");
buf.append("select region_id from region start with region_id = ");
buf.append(region_id);
buf.append(" connect by  parent_id = prior region_id)");
List group_list = dao.queryForList(buf.toString());

%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		
		<base href="<%=basePath%>">

		<title></title>
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="">
		<meta http-equiv="description" content="">
		
		<style type="text/css"> 
			@import "js/tableview/TableView.css";
    	</style>
	</head>
	
	<body>		
		<script type="text/javascript" src="js/jquery.js"></script>
		<script type="text/javascript" src="js/tableview/SortView.js"></script>
		<script type="text/javascript" src="js/tableview/PagerView.js"></script>
		<script type="text/javascript" src="js/tableview/TableView.js"></script>
		<script type="text/javascript" src="js/tableview/SelectorView.js"></script>
		<script type="text/javascript" src="js/tableview/PaperView.js"></script>
		<script type="text/javascript" src="js/utility.js"></script>
		<script type="text/javascript" src="js/calendar.js"></script>
	
	  	<form name="business_tickets_query_form" style="width: 100%"> 
	  	 <table style="width: 100%" >	  	 
		  	<tr>
			  	<td style="<%=style1%>">省</td>
			  	<td>
			  	
			  		<select id="province" name="province" style="<%=style2%>" >
			  		 <option value="">请选择</option>			  		
		             <option value="<%=province_id%>" selected="selected"> <%=province_name%> </option>		             			            
			  		</select>
			  	</td>	
			</tr>
			<tr>
				<td style="<%=style1%>">市</td>  		
			  	<td>	
			  		<select id="city" name="city" style="<%=style2%>" >
			  		<option value="">请选择</option>
					 <%
					 String value;
					 String text;
					 boolean b =false;
					 for(int i = 0; i < city_list.size(); ++i) {
						Map map = (Map)city_list.get(i);
						value = map.get("REGION_ID").toString();
						text = map.get("REGION_NAME").toString();						
						if(region_id.equals(value)) b = true;  //与登陆区域相同，则选中
						if(b==false && i == city_list.size()-1) b = true;  //最后一个都还没有匹配，就默认选中
					 %>
					  <option value="<%=value%>"   <%if(b){%>selected="selected" <%}%> > 
					  <%=text%> 
					  </option>
					 <%}%> 
			  		</select>
			  	</td>
			</tr>
			<tr>
				<td style="<%=style1%>">区</td> 
				<td> 	
			  		<select id="area" name="area" style="<%=style2%>" >
			  		<option value="">请选择</option> 
			  		<%
					 for(int i = 0; i < area_list.size(); ++i) {
						Map map = (Map)area_list.get(i);
						value = map.get("REGION_ID").toString();
						text = map.get("REGION_NAME").toString();												
					 %>
					  <option value="<%=value%>"> 
					  <%=text%> 
					  </option>
					 <%} %> 
			  		</select>
			  	</td>
			</tr>  	
	
			<tr>	  	
			  	<td style="<%=style1%>">商圈</td>
			  	<td>
			  		<select id="group_id" name="group_id" style="<%=style2%>" > 
				  		<option value="">请选择</option> 
				  		<%
						 for(int i = 0; i < group_list.size(); ++i) {
							Map map = (Map)group_list.get(i);
							value = map.get("GROUP_ID").toString();
							text = map.get("NAME").toString();												
						 %>
						  <option value="<%=value%>"> 
						  <%=text%> 
						  </option>
						 <%} %> 
			  		</select>
			  	</td>
			</tr>
			
			<tr>
				<td style="<%=style1%>">商家ID</td>
				<td>
					<input type="text" id="b_business_id" style="<%=style2%>" />
				</td>	
			</tr>
			<tr>
				<td style="<%=style1%>">商家名称</td>
				<td>
					<input type="text" id="b_name" style="<%=style2%>" />
				</td>	
			</tr>
			
			<tr>
				<td style="<%=style1%>">商家核单</td>
				<td>
					<select id="check_b_flag" name="check_b_flag" style="<%=style2%>">
						<option >全部</option>
					   <%
					   String check_flag_value = null;
					   String check_flag_text = null;
					   boolean default_b = false;
					   %>
					   
					   <%
					   for(int n = 0; n < flags.size(); ++n) { 
						   
						   Map tmp = (Map)flags.get(n);
						   check_flag_value = tmp.get("DIC_VALUE").toString();
						   check_flag_text = tmp.get("DIC_TEXT").toString();
						   default_b = check_flag_value.toString().equals("0");
					   %>
					   <option <%if(default_b){%> selected="selected" <%}%>
					     value="<%=check_flag_value%>"><%=check_flag_text%>
					   </option>
					   <%}%>
					</select>	 
				</td>	
			</tr>
			<tr>
				<td style="<%=style1%>">客户核单</td>
				<td>
					<select id="check_c_flag" name="check_c_flag" style="<%=style2%>">
						<option >全部</option>
					   <%
					   check_flag_value = null;
					   check_flag_text = null;
					   default_b = false;
					   %>
					   
					   <%
					   for(int n = 0; n < flags.size(); ++n) { 
						   
						   Map tmp = (Map)flags.get(n);
						   check_flag_value = tmp.get("DIC_VALUE").toString();
						   check_flag_text = tmp.get("DIC_TEXT").toString();
						   default_b = check_flag_value.toString().equals("0");
					   %>
					   <option <%if(default_b){%> selected="selected" <%}%>
					     value="<%=check_flag_value%>"><%=check_flag_text%>
					   </option>
					   <%}%>
					</select>	 
				</td>	
			</tr>
			
			<!-- 下单时间  -->
			<tr>
				<td style="<%=style1%>">下单时间</td>
			  	<td>
			  		<input type="text" id="create_time_d_begin" name="create_time_d_begin" readonly="readonly" style="width: 80px;" onclick="calendar.show(this)" >
			  		<select id="create_time_t_begin" name="create_time_t_begin" style="width: 56px;" >
			  		  <option selected="selected"></option>
			  		  <%
					  for(int n = 0; n < 24; ++n) {
						  
						  value = String.valueOf(n);
						  text = value + "时";
					  %>
					  <option 
					    value="<%=value%>"><%=text%>
					  </option>
					  <%}%>				   
			  		</select>
			  		<font style="font-size: 12px;">至</font>
			  	</td>	
			</tr>
			<tr>
				<td style="<%=style1%>"></td>
				<td>
					<input type="text" id="create_time_d_end" name="create_time_d_end" readonly="readonly" style="width: 80px;" onclick="calendar.show(this)" >
			  		<select id="create_time_t_end" name="create_time_t_end" style="width: 56px;" >
			  		  <option selected="selected"></option>
			  		  <%
					  for(int n = 0; n < 24; ++n) {
						  
						  value = String.valueOf(n);
						  text = value + "时";
					  %>
					  <option 
					    value="<%=value%>"><%=text%>
					  </option>
					  <%}%>				   
			  		</select>
			  	</td>	
			</tr>
			
			<!-- 预约时间  -->
			<tr>
				<td style="<%=style1%>">预约时间</td>
			  	<td>
			  		<input type="text" id="demand_time_d_begin" name="demand_time_d_begin" readonly="readonly" style="width: 80px;" onclick="calendar.show(this)" >
			  		<select id="demand_time_t_begin" name="demand_time_t_begin" style="width: 56px;" >
			  		  <option selected="selected"></option>
			  		  <%
					  for(int n = 0; n < 24; ++n) {
						  
						  value = String.valueOf(n);
						  text = value + "时";
					  %>
					  <option 
					    value="<%=value%>"><%=text%>
					  </option>
					  <%}%>				   
			  		</select>
			  		<font style="font-size: 12px;">至</font>		  					  		
			  	</td>
			</tr>
			<tr>
				<td style="<%=style1%>"></td>
				<td>
					<input type="text" id="demand_time_d_end" name="demand_time_d_end" readonly="readonly" style="width: 80px;" onclick="calendar.show(this)" >
			  		<select id="demand_time_t_end" name="demand_time_t_end" style="width: 56px;" >
			  		  <option selected="selected"></option>
			  		  <%
					  for(int n = 0; n < 24; ++n) {
						  
						  value = String.valueOf(n);
						  text = value + "时";
					  %>
					  <option 
					    value="<%=value%>"><%=text%>
					  </option>
					  <%}%>				   
			  		</select>
				</td>
			</tr>
			
			<tr style="display: none;">
				<td style="<%=style1%>">商家核单操作员</td>
				<td>
					<input type="text" id="check_b_manager" style="<%=style2%>" />
				</td>	
			</tr>
			<tr style="display: none;">
				<td style="<%=style1%>">客户核单操作员</td>
				<td>
					<input type="text" id="check_c_manager" style="<%=style2%>" />
				</td>	
			</tr>
			
			<tr>
				<td colspan="2">
					<input type="button" value="查询" onclick="onQueryBusinessTickets()"/>					
				</td>
			</tr>					
	  	</table>	  	
  	</form>
  	
	<div id="table_div" style= "width: 100%"></div>
	<div id="pager"></div>
	
  	<SCRIPT LANGUAGE="JavaScript">
  	
  		var numPerPage = 10; //每页行数
		var paramValue = "";
		var methodCount = "queryBusinessTicketsCount";
		var methodPage = "queryBusinessTicketsPage";
		
		var table = new TableView('table_div');
		table.header = {
			BUSINESS_ID			: '商家编号',
			RES					: '结果'
		};		
		table.customDefine = {
			BUSINESS_ID			: 'style= "display: none;"',
			RES					: 'style= "cursor: hand;"'
		};
				
		table.dataKey = 'BUSINESS_ID';
		table.display.marker = false;
		table.display.multiple = false;
		table.display.count = false;
		table.title = false;
		table.display.filter = false;
		table.display.pager = false;
		table.display.multiple = false;
		table.pager.size = numPerPage;
		table.dblclick = onClickTable;		

		var pager = new PagerView('pager');
		pager.onclick = onPagerClick;
		pager.itemCount = 0;
		pager.size = numPerPage;
		
		table.render();
		pager.render();

		function queryBusinessTickets(pageNO, returnCount) {
  	  		var begin = (pageNO-1) * numPerPage + 1;
  	  		var end = pageNO * numPerPage;
  	  		var mc = returnCount==true ? methodCount : "";
  	  		var date = {
  	  			BEAN_METHOD 	: "TicketBMO.queryBusinessTickets",
  	  			PARAM_CLASS 	: "java.util.Map",
  	    	  	PARAM_VALUE 	: paramValue,
  	    	  	METHOD_COUNT 	: mc, //为空则不返回记录数
  	    	  	METHOD_PAGE 	: methodPage, //为空则不分页
  	    	  	PAGE_BEGIN 		: begin, //仅在METHOD_PAGE不为空时使用
  	    	  	PAGE_END 		: end
  	  	    };
  	  		$.ajax({
				type: "POST",
				url: "query.exec",
				//data: {id:'123'},
				data: date,
				datatype:"json",
				success: query_success,
				error: show_error
  	  		});
  	  	}
  	  	
  	  	function onPagerClick(pageNO) {
  			queryBusinessTickets(pageNO, "FLASE");
  		}
  	  	
  	  	//刷新当前页
  		function refreshCurrentPage() {
  			queryBusinessTickets(pager.index, "FLASE");
  		}
  			
  		function onQueryBusinessTickets() {
  			//先检查输入  			
  			
  			var region_id = "";
  			var province = document.getElementById("province").value;
  			var city = document.getElementById("city").value;
  			var area = document.getElementById("area").value;
  			
  			if(area != null && area != "") {
  				region_id = area;
  			} else if(city != null && city != "") {
  				region_id = city;
  			} else {
  				region_id = province;
  			}
  			
  			//默认取登陆的
  			if(region_id == null || region_id == "") {
  				region_id = "<%=region_id%>"
  			}
  			
  			//生成查询参数paramValue
  			var tmp;
			paramValue = "{";
			
			paramValue += " STATUS:'" + 99;
			paramValue += "'";
			paramValue += ",";
			
			paramValue += " B_REGION_ID:'" + region_id;  //按目前的逻辑，region_id一直不为空
			paramValue += "'";
			paramValue += ",";
			
  			tmp = document.getElementById("group_id").value;
  			paramValue += " B_GROUP_ID:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("b_business_id").value;
  			paramValue += " B_BUSINESS_ID:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("b_name").value;
  			paramValue += " B_NAME:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("check_b_flag").value;
  			paramValue += " CHECK_B_FLAG:'" + tmp;
			paramValue += "'";
			paramValue += ","; 
			
			tmp = document.getElementById("check_c_flag").value;
  			paramValue += " CHECK_C_FLAG:'" + tmp;
			paramValue += "'";
			paramValue += ",";	
			
			//下单时间
			tmp = document.getElementById("create_time_d_begin").value;
			if(tmp != null && tmp != "") {
				var t = document.getElementById("create_time_t_begin").value;
				if(t == null || t == "") t="0"; //开始时间默认最小0
				
				tmp += " "+t;
			}
			paramValue += " CREATE_TIME_BEGIN:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("create_time_d_end").value;
			if(tmp != null && tmp != "") {
				var t = document.getElementById("create_time_t_end").value;
				if(t == null || t == "") t="23"; //结束时间默认最大23
				
				tmp += " "+t;
			}
			paramValue += " CREATE_TIME_END:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			//预约时间
			tmp = document.getElementById("demand_time_d_begin").value;
			if(tmp != null && tmp != "") {
				var t = document.getElementById("demand_time_t_begin").value;
				if(t == null || t == "") t="0"; //开始时间默认最小0
				
				tmp += " "+t;
			}
			paramValue += " DEMAND_TIME_BEGIN:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("demand_time_d_end").value;
			if(tmp != null && tmp != "") {
				var t = document.getElementById("demand_time_t_end").value;
				if(t == null || t == "") t="23"; //结束时间默认最大23
				
				tmp += " "+t;
			}
			paramValue += " DEMAND_TIME_END:'" + tmp;
			paramValue += "'";					    			
  			  			
  			paramValue += "}";  			
  			
  			//若上述发生更改，记得改下面的onClickTable
  			
  			pager.index = 1;
  	  		queryBusinessTickets(1, true);
  		}
  		
  		function query_success(data) {
  	  		var jdata = eval("(" + data + ")");
  	  		if(jdata.COUNT != null) { //有记录数返回  	  	  		
  	  	  		pager.itemCount = jdata.COUNT;
  	  	  		pager.size = numPerPage;  	  	  		
  	  	  		pager.render();
  	  		} 
		   var array = eval(jdata.LIST);		  
		   table.clear();
		   table.addRange(array);
		}
  		function show_error(data) {
  			var err = "操作失败！\n";
  			err += "status：" + data.status + "\n";
  			err += "statusText：" + data.statusText;
  	  		alert(err);
 		} 		
  	
  		function onClickTable(id) {
  			var region_id = "";
  			var province = document.getElementById("province").value;
  			var city = document.getElementById("city").value;
  			var area = document.getElementById("area").value;
  			
  			if(area != null && area != "") {
  				region_id = area;
  			} else if(city != null && city != "") {
  				region_id = city;
  			} else {
  				region_id = province;
  			}
  			
  			//默认取登陆的
  			if(region_id == null || region_id == "") {
  				region_id = "<%=region_id%>"
  			}
  			
  			//生成查询参数paramValue
  			var tmp;
			paramValue = "{";			
			
			paramValue += " STATUS:'" + 99;
			paramValue += "'";
			paramValue += ",";			
			
  			paramValue += " BUSINESS_ID:'" + id;
			paramValue += "'";
			paramValue += ",";			
			
			tmp = document.getElementById("check_b_flag").value;
  			paramValue += " CHECK_B_FLAG:'" + tmp;
			paramValue += "'";
			paramValue += ","; 
			
			tmp = document.getElementById("check_c_flag").value;
  			paramValue += " CHECK_C_FLAG:'" + tmp;
			paramValue += "'";
			paramValue += ","; 
			
			//下单时间
			tmp = document.getElementById("create_time_d_begin").value;
			if(tmp != null && tmp != "") {
				var t = document.getElementById("create_time_t_begin").value;
				if(t == null || t == "") t="0"; //开始时间默认最小0
				
				tmp += " "+t;
			}
			paramValue += " CREATE_TIME_BEGIN:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("create_time_d_end").value;
			if(tmp != null && tmp != "") {
				var t = document.getElementById("create_time_t_end").value;
				if(t == null || t == "") t="23"; //结束时间默认最大23
				
				tmp += " "+t;
			}
			paramValue += " CREATE_TIME_END:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			//预约时间
			tmp = document.getElementById("demand_time_d_begin").value;
			if(tmp != null && tmp != "") {
				var t = document.getElementById("demand_time_t_begin").value;
				if(t == null || t == "") t="0"; //开始时间默认最小0
				
				tmp += " "+t;
			}
			paramValue += " DEMAND_TIME_BEGIN:'" + tmp;
			paramValue += "'";
			paramValue += ",";
			
			tmp = document.getElementById("demand_time_d_end").value;
			if(tmp != null && tmp != "") {
				var t = document.getElementById("demand_time_t_end").value;
				if(t == null || t == "") t="23"; //结束时间默认最大23
				
				tmp += " "+t;
			}
			paramValue += " DEMAND_TIME_END:'" + tmp;
			paramValue += "'";				    			
  			  			
  			paramValue += "}";  
  			
  			var url = "<%=url_right%>" + "?business_id="+id + "&param="+paramValue;
  			parent.document.getElementById("rightFrame").src = url;
  		}
  	</SCRIPT>
	</body>
</html>
